<!DOCTYPE HTML>
<html>
<head>
 <title>DOM Traversal: NodeIterator: Mutations (3/x)</title>
</head>
<p id="display"></p>
<div id="content" style="display: none">
  <span id=root><span id=B></span><span id=C></span><span id=D></span><span id=E><span id=E1><span id=E11></span></span></span></span>
</div>
<pre id="test">
Results available in the JavaScript console
Test adapted from http://mxr.mozilla.org/mozilla-central/source/content/base/test/test_NodeIterator_mutations_3.html
<script class="testbody" type="text/javascript">
  if (window.testRunner)
    testRunner.dumpAsText();

  function $(id) {
    return document.getElementById(id);
  }

  function removeNode(n) {
    n.parentNode.removeChild(n);
  }
  var initInner = $('content').innerHTML;
  var content = $('content');

  function resetContent() {
    content.innerHTML = initInner;
    var checkIt = document.createNodeIterator(content, NodeFilter.SHOW_ELEMENT, testNodeFilter, false);
    var node;
    while ((node = checkIt.nextNode()) != null) {
      if (node.id) {
        window[node.id] = node;
      }
    }
  }

  function makeSpan(id) {
    var e = document.createElement('span');
    e.id = id;
    return e;
  }

  function testNodeFilter(n) {
    if (n.tagName == 'SPAN')
      return NodeFilter.FILTER_ACCEPT;
    return NodeFilter.FILTER_SKIP;
  }

  function checkseq(it, root, expect) {
    var checkIt = document.createNodeIterator(root, NodeFilter.SHOW_ELEMENT, testNodeFilter, false);
    var printedPointer = (it.referenceNode == undefined);
    var string = '';
    var node;
    while ((node = checkIt.nextNode()) != null) {
      if (!printedPointer && it.referenceNode == node) {
        printedPointer = true;
        var s = '[' + node.id + '] ';
          if (it.pointerBeforeReferenceNode)
            string += "* " + s;
          else
            string += s + "* ";
      } else {
        string += node.id + " ";
      }
    }
    console.log("sequence check:");
    console.log(string.slice(0, -1) + " (actual)");
    console.log(expect + " (expected)");
  }

  resetContent();
  var it = document.createNodeIterator(E, NodeFilter.SHOW_ELEMENT,
                                       testNodeFilter, false);
  checkseq(it, root, "root B C D * [E] E1 E11");

  removeNode(C);
  checkseq(it, root, "root B D * [E] E1 E11");

  it.nextNode();
  removeNode(D);
  checkseq(it, root, "root B [E] * E1 E11");

  it.nextNode();
  removeNode(B);
  checkseq(it, root, "root E [E1] * E11");

  it.nextNode();
  checkseq(it, root, "root E E1 [E11] *");

  it.nextNode();
  checkseq(it, root, "root E E1 [E11] *");

  it.previousNode();
  it.previousNode();
  it.previousNode();
  it.previousNode();
  it.previousNode();
  checkseq(it, root, "root * [E] E1 E11");

  resetContent();
  it = document.createNodeIterator(E, NodeFilter.SHOW_ELEMENT,
                                   testNodeFilter, false);
  checkseq(it, root, "root B C D * [E] E1 E11");

  it.nextNode();
  it.nextNode();
  checkseq(it, root, "root B C D E [E1] * E11");

  it.previousNode();
  it.previousNode();
  checkseq(it, root, "root B C D * [E] E1 E11");

  removeNode(D);
  removeNode(B);
  checkseq(it, root, "root C * [E] E1 E11");

  n = makeSpan('n');
  root.insertBefore(n, E);
  checkseq(it, root, "root C n * [E] E1 E11");

  n2 = makeSpan('n2');
  root.insertBefore(n2, C);
  checkseq(it, root, "root n2 C n * [E] E1 E11");

  resetContent();
  it = document.createNodeIterator(E, NodeFilter.SHOW_ELEMENT,
                                   testNodeFilter, false);
  checkseq(it, root, "root B C D * [E] E1 E11");

  removeNode(root);
  checkseq(it, root, "root B C D * [E] E1 E11");

  removeNode(B);
  checkseq(it, root, "root C D * [E] E1 E11");

  removeNode(D);
  checkseq(it, root, "root C * [E] E1 E11");

  it.nextNode();
  it.nextNode();
  it.nextNode();
  checkseq(it, root, "root C E E1 [E11] *");

  removeNode(E1);
  checkseq(it, root, "root C [E] *");
  
  n = makeSpan('n');
  root.insertBefore(n, E);
  checkseq(it, root, "root C n [E] *");
  
  n2 = makeSpan('n2');
  E.appendChild(n2);
  checkseq(it, root, "root C n [E] * n2");
  
  it.nextNode();
  checkseq(it, root, "root C n E [n2] *");

  removeNode(E);
  checkseq(it, root, "root C n");

  
</script>
</pre>
</body>
</html>
